﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AGameOfThrones.piece
{
    /// <summary>
    /// Interaction logic for piece.xaml
    /// </summary>
    public partial class piece : UserControl
    {
        public Point DragStart { get; set; }
        public bool IsDragging { get; set; }
        public String PieceName { get; set; }
        
        public ui.manager UIManager
        {
            get  
            {
                try
                {
                    return System.Windows.Application.Current.FindResource("uimanager") as ui.manager;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }

            }
        } 
        
        public piece()
        {
            InitializeComponent();
        }

        public bool FrontVisible
        {
            get { return Front.Visibility == Visibility.Visible; }
            set 
            {
                if( value != (Front.Visibility == Visibility.Visible) )
                {
                    Front.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                    Back.Visibility = !value ? Visibility.Visible : Visibility.Hidden;
                }
               
            }
        }

        public bool BackVisible
        {
            get { return Back.Visibility == Visibility.Visible; }
            set
            {
                if (value != (Back.Visibility == Visibility.Visible))
                {
                    Back.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                    Front.Visibility = !value ? Visibility.Visible : Visibility.Hidden;
                }

            }
        }

        private void piece_control_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            UIElement uielement = sender as UIElement;
            if (e.LeftButton == MouseButtonState.Pressed && !IsDragging)
            {
                Point position = e.GetPosition(uielement);

                if (Math.Abs(position.X - DragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - DragStart.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    IsDragging = true;
                    FindDragManager(uielement).ExecuteDrag(uielement, e);
                    IsDragging = false;
                }
            } 
        }

        private void piece_control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            UIElement uielement = sender as UIElement;
            DragStart = e.GetPosition(uielement);
            e.Handled = true;
        }

        private drag.manager FindDragManager(UIElement uielement)
        {
            DependencyObject parent = uielement as DependencyObject;
            while (parent != null)
            {
                if (parent.GetType() == typeof(drag.manager))
                {
                    drag.manager m = parent as drag.manager;
                    return m;
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            return null;
        }

        private void piece_control_Loaded(object sender, RoutedEventArgs e)
        {
            if (PieceName != null)
            {
                if (UIManager.Pieces.ContainsKey(PieceName))
                {
                    UIManager.Pieces[PieceName] = this;
                }
                else
                {
                    UIManager.Pieces.Add(PieceName, this);
                }
            }
        }
    }
}
